﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeamTime.Classes;

namespace TeamTime
{
    public partial class Rpt_TimeSheet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Get the first of the current month
            DateTime dt = DateTime.Today.AddDays(-1 * (DateTime.Today.Day - 1));
            this.txtFromDate.Text = dt.AddMonths(-1).ToString("yyyy/MM/dd"); // First day of last month
            this.txtToDate.Text = dt.AddDays(-1).ToString("yyyy/MM/dd");// Last day of last month

        }

        protected void btnViewReport_Click(object sender, EventArgs e)
        {
            Int32 staffMemberID = 0; Int32 companyID = 0;
            DateTime fromdate;
            DateTime todate;
            ReportToFileControl1.HeaderTxt = "";
            if (txtFromDate.Text == "" && txtToDate.Text == "")
            {
                fromdate = Convert.ToDateTime("1999-09-09");
                todate = Convert.ToDateTime("3000-03-03");
               
            }
            else
            {
                if (txtFromDate.Text == string.Empty)
                {
                    fromdate = Convert.ToDateTime("1999-09-09");
                    ReportToFileControl1.HeaderTxt = "To: " + Convert.ToDateTime(txtToDate.Text).ToString("dd MMM yyyy") + "\n";
                }
                else { fromdate = Convert.ToDateTime(txtFromDate.Text); }
                if (txtToDate.Text == string.Empty)
                {
                    todate = Convert.ToDateTime("3000-03-03");
                    ReportToFileControl1.HeaderTxt ="From: " + Convert.ToDateTime(txtFromDate.Text).ToString("dd MMM yyyy") + "\n";
                }
                else { todate = Convert.ToDateTime(txtToDate.Text); }
                if (txtFromDate.Text != "" && txtToDate.Text != "")
                {
                    ReportToFileControl1.HeaderTxt = "From: " + Convert.ToDateTime(txtFromDate.Text).ToString("dd MMM yyyy") + " "
                    + "To: " + Convert.ToDateTime(txtToDate.Text).ToString("dd MMM yyyy") + "\n";
                }
            }
            if (txtStaffMember.Text != "")
            {
                StaffMembers staffMembers = new StaffMembers();
                staffMemberID = staffMembers.getStaffMamberIDFromName(this.txtStaffMember.Text);
                ReportToFileControl1.HeaderTxt += "Person: " + txtStaffMember.Text + "\n";
            }
            if (txtCompany.Text != "")
            {
                Companies companies = new Companies();
                companyID = companies.GetCompanyIDFromName(this.txtCompany.Text);
                ReportToFileControl1.HeaderTxt += "For client: " + txtCompany.Text + "\n";
            }
            if (this.txtInvoiceNum.Text != "")
            {
                ReportToFileControl1.HeaderTxt += "For invoice: " + txtInvoiceNum.Text + "\n";
            }

            TimeEntries timeEntries = new TimeEntries();
            ReportToFileControl1.MainReportDataSet = timeEntries.getTimeSheetEntries(fromdate, todate, companyID, staffMemberID, 
                                                                                     txtInvoiceNum.Text, this.ddlStatusFilter.SelectedValue);
            ReportToFileControl1.Column = "";
            ReportToFileControl1.MainReportDataSetName = "TeamTimeDataSet_ViewTimeEntries";
            ReportToFileControl1.PDFFileName = "TimeSheet";
            ReportToFileControl1.ReportName = "TimeSheet.rdlc";
            ReportToFileControl1.RenderReport();
        }
    }
}
